<!--
  - Author: Ray (qray686898@163.com)
  -->

<template>
  <div class="top">
    <div class="top-button is-left">
      <div class="tags-breadcrumb">
        <i class="icon-navicon tag-collapse" :class="[{ 'tag-collapse_right': isCollapse }]" @click="showCollapse"></i>
      </div>
    </div>
    <h1 class="top-title">
      <topMenu></topMenu>
    </h1>
    <div class="top-button is-right">
      <el-tooltip class="item" effect="dark" content="主题色" placement="bottom">
        <span class="top-item">
          <top-theme></top-theme>
        </span>
      </el-tooltip>
      <el-tooltip class="item" effect="dark" content="锁屏" placement="bottom">
        <span class="top-item">
          <top-lock></top-lock>
        </span>
      </el-tooltip>
      <el-tooltip class="item" effect="dark" :content="isFullScren?'退出全屏':'全屏'" placement="bottom">
        <span class="top-item">
          <i :class="isFullScren?'icon-tuichuquanping':'icon-quanping'" @click="handleScreen"></i>
        </span>
      </el-tooltip>
      <el-tooltip class="item" effect="dark">
        <el-badge :value="todayCount" class="item" v-if="todayCount>0">
          <el-button size="mini" @click="showWork">待办</el-button>
        </el-badge>
      </el-tooltip>
      <el-dropdown>
        <span class="el-dropdown-link">
          {{companyName}}
          <i class="el-icon-arrow-down el-icon--right"></i>
        </span>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item v-for="(company,index) in userInfo.companys" :key="index">
            <div class="c-box" @click="doSwitch(company.companyCode)">{{company.companyName}}</div>
          </el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
      <el-tooltip class="item" effect="dark" content="用户头像" placement="bottom">
        <img class="top-userImg" :src="userInfo.headUrl">
      </el-tooltip>
      <el-dropdown>
        <span class="el-dropdown-link">
          {{userInfo.userName}}
          <i class="el-icon-arrow-down el-icon--right"></i>
        </span>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item>
            <router-link to="/wel">首页</router-link>
          </el-dropdown-item>
          <el-dropdown-item>
            <router-link to="/info/index">修改信息</router-link>
          </el-dropdown-item>
          <el-dropdown-item>
            <router-link to="/password/index">密码修改</router-link>
          </el-dropdown-item>
          <el-dropdown-item @click.native="logout" divided>退出系统</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
      <GridDialog ref="workGridDialog" :multi="false" title="今日待办" :url="workUrl" :columns="columns" :queryEntity="{workStart: new Date(),workEnd:new Date()}" :showBtn="false"
        :modal="false">
      </GridDialog>
    </div>
  </div>
</template>
<script>
import { mapState, mapGetters } from "vuex";
import { fullscreenToggel, listenfullscreen } from "@/util/util";
import topLock from "./top-lock";
import topMenu from "./top-menu";
import topTheme from "./top-theme";
import request from '@/router/axios'
import url from '@/const/apiURL'
import GridDialog from '@/components/GridDialog'


export default {
  components: { topLock, topMenu, topTheme, GridDialog },
  name: "top",
  data () {
    return {
      companyName: '',
      todayCount: 0,
      workUrl: url.WORK_LIST_URL,
      columns: [
        { label: '待办标题', width: 160, prop: 'workTitle', align: 'left' },
        { label: '内容', width: 300, prop: 'workContent', align: 'center' },
        { label: '开始时间', width: 160, prop: 'workStartTime', align: 'center' },
        { label: '结束时间', width: 160, prop: 'workEndTime', align: 'center' },
        { label: '创建人', width: 100, prop: 'createUserName', align: 'center' },
        { label: '创建时间', width: 100, prop: 'createTime', align: 'center' }
      ]
    }
  },
  filters: {},
  created () {
    this.companyName = this.userInfo.companyName
    this.init()
  },
  mounted () {
    listenfullscreen(this.setScreen);
  },
  computed: {
    ...mapState({
      userInfo: state => state.user.userInfo
    }),
    ...mapGetters(["isFullScren", "isCollapse"])
  },
  methods: {
    init () {
      this.workToday().then(data => {
        if (data) {
          this.todayCount = data.length
        }
      })
    },
    showCollapse () {
      this.$store.commit("SET_COLLAPSE");
    },
    showWork () {
      this.$refs['workGridDialog'].show()
    },
    handleScreen () {
      fullscreenToggel();
    },
    setScreen () {
      this.$store.commit("SET_FULLSCREN");
    },
    doSwitch (companyCode) {
      this.switchOp(companyCode).then(data => {
        if (data) {
          location.reload();
        }
      })
    },
    workToday () {
      return request({
        url: url.WORK_TODAY_URL,
        method: 'post',
        data: { 'key': 'key' }
      })
    },
    switchOp (code) {
      return request({
        url: url.SWITCH_URL,
        method: 'post',
        data: { 'key': 'key' },
        params: { companyCode: code }
      })
    },
    logout () {
      this.$confirm("是否退出系统, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.$store.dispatch("LogOut").then(() => {
          this.$router.push({ path: "/login" });
        });
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.c-box {
  height: 24px;
  padding: 2px;
}
</style>

